package com.cos.openapi.services.impl;

import com.cos.openapi.dao.AdminUserMapper;
import com.cos.openapi.entity.AdminUser;
import com.cos.openapi.pojo.JsonResult;
import com.cos.openapi.pojo.TableResult;
import com.cos.openapi.services.AdminUserServices;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.xml.crypto.dsig.keyinfo.PGPData;
import java.util.List;
import java.util.Map;

/**
 * @author: COS
 * @time: 2021/1/29 9:20
 * @description:
 */
@Service
public class AdminUserServicesImpl implements AdminUserServices {
    @Autowired
    private AdminUserMapper adminUserMapper;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return 0;
    }

    @Override
    public int insertSelective(AdminUser record) {
        return 0;
    }

    @Override
    public AdminUser selectByPrimaryKey(Integer id) {
        return null;
    }

    @Override
    public int updateByPrimaryKeySelective(AdminUser record) {
        return 0;
    }

    @Override
    public JsonResult doLogin(String email, String password) {
        //参数不能为空
        if (StringUtils.hasLength(email)&&StringUtils.hasLength(password)) {
            //去数据库查询数据
            AdminUser adminUserByEmail = adminUserMapper.getAdminUserByEmail(email);
            //判断是否查到数据,并且查到的数据密码和密码进行比对
            if (adminUserByEmail != null&&adminUserByEmail.getPassword().equals(password)) {
                return JsonResult.getJsonResultBySuccess(adminUserByEmail);
            }
        }
        return JsonResult.getJsonResultByFail("用户或者密码有误", null);
    }

    @Override
    public TableResult table(Map<String, Object> map) {

        System.out.println(map);

        String realName = (String) map.get("realName");
        //null
        String p = (String) map.get("page");
        //设置默认值
        Integer page =1;
        if (p != null) {
            page= Integer.valueOf(p);
        }
        String l = (String) map.get("limit");
        //设置默认值
        Integer limit =5;
        if (l != null) {
            limit= Integer.valueOf(l);
        }
        String s = (String) map.get("status");
        Integer status =null;
        if (StringUtils.hasLength(s)) {
            status= Integer.valueOf(s);
        }
        String email = (String) map.get("email");
        PageHelper.startPage(page, limit);
        List<AdminUser> users = adminUserMapper.table(realName,email,status);
        PageInfo<AdminUser> info=new PageInfo<>(users);
        return TableResult.getResultBySuccess(info.getTotal(), info.getList());
    }

    @Override
    public JsonResult del(Integer[] ids) {
        if (ids!=null&&ids.length>0) {
            //删除用户数据
            adminUserMapper.del(ids);
            //删除用户权限数据
            adminUserMapper.delRoleByUserIds(ids);
            return JsonResult.getJsonResultBySuccess("删除成功");
        }
        return JsonResult.getJsonResultByFail("删除失败");
    }

    @Override
    public List<Integer> user_role(Integer userId) {
        if (userId>0) {
            return adminUserMapper.user_role(userId);
        }
        return null;
    }

    @Override
    public JsonResult assign_role(Integer userId, Integer[] roleIds) {
        try {
            //1 删除用户原来放入权限数据
            adminUserMapper.delRole(userId);
            //2 新增用户先要权限数据
            adminUserMapper.addRole(userId,roleIds);
            return JsonResult.getJsonResultBySuccess("更新权限成功");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return JsonResult.getJsonResultByFail("更新权限失败");
    }

    @Override
    public JsonResult add(AdminUser adminUser) {
        if (adminUser != null) {
            if (adminUserMapper.insertSelective(adminUser)>0) {
                return JsonResult.getJsonResultBySuccess("新增成功");
            }
        }
        return JsonResult.getJsonResultByFail("新增失败");
    }

    @Override
    public JsonResult update(AdminUser adminUser) {
        if (adminUser != null) {
            if (adminUserMapper.updateByPrimaryKeySelective(adminUser)>0) {
                return JsonResult.getJsonResultBySuccess("修改成功");
            }
        }
        return JsonResult.getJsonResultByFail("修改失败");
    }
}
